////////

#if 1
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

#include <que_struct_type.h>

#if 0
static void InitItmTypeQue(defTypeQue *q)
{
	q->front = q->rear = 0;
}
#endif
//static int getRtcMaxCount(void);
static void que_push_back(defTypeQue *q, void* e);
//static void que_push_back(defTypeQue *q,uint16_t e);
static bool que_pop_front(defTypeQue *q, void* e);

static bool que_get_index(defTypeQue *q, uint16_t i, void* v);
static int que_length(defTypeQue *q);

#if 0
typedef struct
{
	enmTypeQue nenm;
	int nsize;
} defEnmTypeSize;

static const defEnmTypeSize m_type_size[]=
{
	{ enmTypeU08,      1 },
	{ enmTypeU16,      2 },
	{ enmTypeU32,      4 },
	{ enmTypeLaserCpu, 5 },

	{ enmTypeUnDef,    4 },
};

static int get_size_enm_que(defTypeQue *q)
{
		//defCarStatus* pCarStatus = NULL;
	int i=0;
	for(i=0;i<(int)(sizeof(m_type_size)/sizeof(defEnmTypeSize)-1);i++)
	{
		if(m_type_size[i].nenm==q->nenm)
		{
			return m_type_size[i].nsize;
			//pCarStatus = &m_nCarStatus[i];
			//return pCarStatus;
			break;
		}
	}
	return 4;
}
#endif

static void que_push_back(defTypeQue *q,void* e)
{
	q->rear = (q->rear + 1) % q->countMax;
	//memcpy( q->pData+q->rear*sizeof(q->pData[q->rear] ), e, sizeof(q->pData[q->rear]) );
	memcpy( q->pData + q->nsize*q->rear, e, q->nsize );
	//memcpy( &q->pData[q->rear], e, 2 );

	//memcpy( &(q->pData[q->rear]), e, sizeof(q->pData[q->rear]) );
	#if 0
	printf("%s,%s,%d,%ld;%ld\r\n", __FILE__,__func__,__LINE__, sizeof(q->pData[q->rear]),q->rear*sizeof(q->pData[q->rear]) );

	printf("%s,%s,%d,%ld;%ld;%ld;%ld\r\n", __FILE__,__func__,__LINE__, 
		sizeof(q->pData[q->rear]),q->rear*sizeof(q->pData[q->rear]), sizeof(int), sizeof(uint16_t) );
	#endif

	if( q->rear == q->front )
	{
		q->front = (q->front + 1) % q->countMax;
	}

}

static bool que_pop_front(defTypeQue *q,void* e)
{
	if( q->rear < 0 )
		return false;
	memcpy( e, q->pData + q->nsize*q->front, q->nsize );
	
	if( q->rear == q->front )
	{
		q->rear = -1;
		q->front = 0;
	}
	else
	{
		q->front = (q->front + 1) % q->countMax;
	}
		
	return true;
}

static bool que_get_index(defTypeQue *q,uint16_t i,void* v)
{
	if(q->rear < 0)
		return false;
	if(que_length(q) <= i) return false;

	printf("%s,%s,%d,%d,%d\r\n", __FILE__,__func__,__LINE__, ( (q->front+i) % q->countMax ), q->front+i );
	memcpy( v, q->pData + q->nsize*( (q->front+i) % q->countMax ), q->nsize );
	return true;
}
static int que_length(defTypeQue *q)
{	
	if(q->rear < 0)
		return 0;
	
	return ( ( q->rear + q->countMax - q->front ) % q->countMax ) + 1;
#if 0	
	if( q->rear >= q->front )
		return q->rear - q->front + 1;
	return q->rear + q->countMax - q->front + 1;
	//return ( ( q->rear + q->countMax - q->front ) % q->countMax ) + 1;
#endif	
}

//const int CRtcMaxSIZE=5;
#define CRtcMaxSIZE 5
//static const  uint16_t CRtcMaxSIZE=5;
//uint16_t const CRtcMaxSIZE=5;
//static const uint16_t CRtcMaxSIZE=5;
static uint16_t RtcData[CRtcMaxSIZE];

static defTypeQue RtcQue=
{
    //.count=CRtcMaxSIZE,
    .front=0,
    .rear=-1,
	.nsize=2, //sizeof(uint16_t)
	//.size=sizeof(RtcData)/sizeof(uint16_t)+1,
	.countMax=CRtcMaxSIZE, //+1,
	.pData=&RtcData,

    //.getQueCount=getRtcMaxCount,
    .push_back=que_push_back,
    .pop_front=que_pop_front,
    .get_index=que_get_index,
    .length=que_length,
    //.pData=&RtcData[0],
};

defTypeQue* getRtcQuePt(void)
{
	printf("%s,%s,%d,%ld;%ld\r\n", __FILE__,__func__,__LINE__, sizeof(RtcData) ,sizeof(RtcQue) );
    return &RtcQue;
}

void prtRtcQue(void)
{
	for( int i=0; i<CRtcMaxSIZE; i++ )
	{
		printf("%s,%s,%d,0x%08X\r\n", __FILE__,__func__,__LINE__, RtcData[i] );
	}


}

#endif



